﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;
namespace DAO
{
    public class LoaiPhongDAO: Abstract
    {
        public static bool ThemLoaiPhong(LoaiPhongDTO loaiPhong)
        {
            string query = "insert into LoaiPhong (TenLoaiPhong, DonGia) values ('{1}',{2})"
                .Replace("{1}", loaiPhong.TenLoaiPhong)
                .Replace("{2}", loaiPhong.DonGia.ToString());
            return Query(query);
        }
        public static bool SuaLoaiPhong(LoaiPhongDTO loaiPhong)
        {
            string query = "update LoaiPhong set TenLoaiPhong = '{1}', DonGia = {2} where MaLoaiPhong = {0}"
                .Replace("{0}", loaiPhong.MaLoaiPhong.ToString())
                .Replace("{1}", loaiPhong.TenLoaiPhong)
                .Replace("{2}", loaiPhong.DonGia.ToString());
            return Query(query);
        }
        public static bool XoaLoaiPhong(int _maLoaiPhong)
        {
            string query = "delete from LoaiPhong where MaLoaiPhong = {1}"
                .Replace("{1}", _maLoaiPhong.ToString());
            return Query(query);
        }
        public static List<LoaiPhongDTO> LayDSLoaiPhong(int _maLoaiPhong = -1, string _tenLoaiPhong = null)
        {
            OleDbConnection conn = null;
            List<LoaiPhongDTO> listLoaiPhong = new List<LoaiPhongDTO>();
            try
            {
                conn = GetConnection();
                conn.Open();
            }
            catch
            {
                throw new Exception("Connect error!");
            }
            try
            {
                string query = "select MaLoaiPhong, TenLoaiPhong, DonGia from LoaiPhong ";
                if (_maLoaiPhong != -1)
                    query = query + "where MaLoaiPhong = {0}".Replace("{0}", _maLoaiPhong.ToString());
                else if (_tenLoaiPhong != null)
                    query = query + "where TenLoaiPhong = '{1}'".Replace("{1}", _tenLoaiPhong);
   
                OleDbDataReader rd = new OleDbCommand(query, conn).ExecuteReader();
                while (rd.Read())
                {
                    LoaiPhongDTO loaiphong = new LoaiPhongDTO();

                    loaiphong.MaLoaiPhong = rd.GetInt32(0);
                    loaiphong.TenLoaiPhong = rd.GetString(1);
                    loaiphong.DonGia = rd.GetInt32(2);

                    listLoaiPhong.Add(loaiphong);
                }
            }
            catch (Exception e)
            {
                listLoaiPhong = new List<LoaiPhongDTO>();
            }

            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
            return listLoaiPhong;
        }
    }
}
